package com.konsung.ksdc.manager.saleservice;

import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.konsung.ksdc.R;
import com.konsung.ksdc.bean.netbean.MachineNetBean;
import com.konsung.ksdc.bean.netbean.SaleServiceNetBean;
import com.konsung.ksdc.common.utils.Constants;
import com.konsung.ksdc.config.MyApplication;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 售后list适配器
 * Created by Administrator on 2016/3/24 0024.
 */
public class ProblemListAdapter extends BaseAdapter {

    private List<SaleServiceNetBean> saleServiceNetBeanList;

    /**
     * 构造方法
     */
    public ProblemListAdapter() {
    }

    @Override
    public int getCount() {
        if (saleServiceNetBeanList != null) {
            return saleServiceNetBeanList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        return saleServiceNetBeanList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(MyApplication.getContext())
                    .inflate(R.layout.item_listview_problem, null);
            ButterKnife.bind(holder, convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        SaleServiceNetBean bean = saleServiceNetBeanList.get(position);

        // 提交日期
        String createDate = bean.getCreateDate();
        String date = createDate.replace("&", " ").replace("时", ":").replace("分", ":")
                .replace("秒", "");
        holder.tvCreateDate.setText(date);

        // 故障内容
        holder.tvServiceContent.setText(bean.getServiceContent());

        // 机器名称
        MachineNetBean macDto = bean.getMacDto();
        if (macDto != null) {
            holder.tvMacName.setText(macDto.getMacName());
        }
        if (bean.getIsReply().equals(Constants.afterSellReplayed)) {
            holder.ivHealthOne.setImageResource(R.drawable.ic_health_one_sel);
        } else {
            holder.ivHealthOne.setImageResource(R.drawable.ic_health_one);
        }
        //处理状态
        String status = bean.getServiceState();
        switch (status) {
            case SaleServiceNetBean.STATUS_UNDISPOSED:
                holder.ivStatus.setImageResource(R.drawable.label_waiting);
                holder.tvApplyTimeText.setTextColor(ContextCompat.getColor(MyApplication
                        .getContext(), R.color.title_text_bg_pressed));
                holder.tvCreateDate.setTextColor(ContextCompat.getColor(MyApplication
                        .getContext(), R.color.title_text_bg_pressed));
                break;
            case SaleServiceNetBean.STATUS_DISPOSING:
                holder.ivStatus.setImageResource(R.drawable.label_dealing);
                holder.tvApplyTimeText.setTextColor(ContextCompat.getColor(MyApplication
                        .getContext(), R.color.title_text_bg_pressed));
                holder.tvCreateDate.setTextColor(ContextCompat.getColor(MyApplication
                        .getContext(), R.color.title_text_bg_pressed));
                break;
            case SaleServiceNetBean.STATUS_DISPOSED:
                holder.ivStatus.setImageResource(R.drawable.label_dealing);
                holder.tvApplyTimeText.setTextColor(ContextCompat.getColor(MyApplication
                        .getContext(), R.color.title_text_bg_pressed));
                holder.tvCreateDate.setTextColor(ContextCompat.getColor(MyApplication
                        .getContext(), R.color.title_text_bg_pressed));
                break;
            case SaleServiceNetBean.STATUS_USER_CONFIRM:
                holder.ivStatus.setImageResource(R.drawable.label_feedback);
                holder.tvApplyTimeText.setTextColor(ContextCompat.getColor(MyApplication
                        .getContext(), R.color.title_text_bg_pressed));
                holder.tvCreateDate.setTextColor(ContextCompat.getColor(MyApplication
                        .getContext(), R.color.title_text_bg_pressed));
                break;
            case SaleServiceNetBean.STATUS_CLOSED:
                holder.ivStatus.setImageResource(R.drawable.label_closed);
                holder.tvApplyTimeText.setTextColor(ContextCompat.getColor(MyApplication
                        .getContext(), R.color.order_not_send));
                holder.tvCreateDate.setTextColor(ContextCompat.getColor(MyApplication
                        .getContext(), R.color.order_not_send));
                break;
            default:
                break;
        }
        return convertView;
    }

    /**
     * 适配器类
     */
    static class ViewHolder {
        @Bind(R.id.tv_create_date)
        TextView tvCreateDate;
        @Bind(R.id.tv_mac_name)
        TextView tvMacName;
        @Bind(R.id.tv_service_content)
        TextView tvServiceContent;
        @Bind(R.id.iv_health_one)
        ImageView ivHealthOne;
        @Bind(R.id.iv_status)
        ImageView ivStatus;
        @Bind(R.id.tv_apply_time_text)
        TextView tvApplyTimeText;
    }

    /**
     * 设置更新数据
     * @param list 售后集合list
     */
    public void setData(List<SaleServiceNetBean> list) {
        saleServiceNetBeanList = list;
    }

    /**
     * 设置更新数据
     * @param list 售后集合list
     */
    public void refreshData(List<SaleServiceNetBean> list) {

        saleServiceNetBeanList.clear();
        saleServiceNetBeanList = list;
        notifyDataSetChanged();
    }
}
